package com.shiwaixiangcun.customer.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.shiwaixiangcun.customer.R;
import com.shiwaixiangcun.customer.utils.DisplayUtil;


/**
 * Author   Administrator
 * Date     2018/4/12
 * Desc     eg
 *
 * @author Administrator
 */
public class PayDialog extends DialogFragment implements View.OnClickListener {

    public static final int PAY_WEXIN = 1;
    public static final int PAY_ALI = 2;
    ImageView mIvClose;
    TextView mTvNeedPay;
    CheckBox mCbWeixin;
    CheckBox mCbZhifubao;
    Button mBtnPay;
    private Context mContext;
    private int defaultPay = PAY_WEXIN;
    private OnPayClick listener;
    private Activity mActivity;
    private boolean isShow;
    private String shouldPay;

    public int getDefaultPay() {
        return defaultPay;
    }

    public void setListener(OnPayClick listener) {
        this.listener = listener;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isShow = true;
        mActivity = this.getActivity();
        setStyle(android.support.v4.app.DialogFragment.STYLE_NO_TITLE, R.style.UpdateAppDialog);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mActivity = null;
    }


    public void showDialog(FragmentManager manager, String tag) {
        try {
            //在每个add事务前增加一个remove事务，防止连续的add
            manager.beginTransaction().remove(this).commit();
            super.show(manager, tag);
        } catch (Exception e) {
            //同一实例使用不同的tag会异常,这里捕获一下
            e.printStackTrace();
        }
    }
    @Override
    public void onStart() {
        super.onStart();
        //点击window外的区域 是否消失
        int margin = DisplayUtil.INSTANCE.dip2px(mActivity, 0);
        getDialog().setCanceledOnTouchOutside(true);
        Window window = getDialog().getWindow();
        assert window != null;
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(margin, 8, margin, margin);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (null != bundle) {
            shouldPay = bundle.getString("shouldPay");
        }
        return inflater.inflate(R.layout.dialog_pay, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        mIvClose = view.findViewById(R.id.iv_close);
        mTvNeedPay = view.findViewById(R.id.tv_need_pay);
        mCbWeixin = view.findViewById(R.id.cb_weixin);
        mCbZhifubao = view.findViewById(R.id.cb_zhifubao);
        mBtnPay = view.findViewById(R.id.btn_pay);
        mBtnPay.setOnClickListener(this);
        mIvClose.setOnClickListener(this);
        mCbZhifubao.setChecked(false);
        mCbWeixin.setChecked(true);
        mCbZhifubao.setOnCheckedChangeListener(new ZhifubaoOncheckListener());
        mCbWeixin.setOnCheckedChangeListener(new weixinOnCheckListener());
        mTvNeedPay.setText("￥" + shouldPay);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_close:

                dismiss();
                break;
            case R.id.btn_pay:
                if (listener != null) {
                    listener.confirmPay(this);
                }
                break;
            default:
                break;
        }
    }

    public interface OnPayClick {


        /**
         * 确认支付
         *
         * @param dialog
         */
        void confirmPay(PayDialog dialog);
    }

    public class weixinOnCheckListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b) {
                mCbZhifubao.setChecked(false);
                mCbWeixin.setChecked(true);
                defaultPay = PAY_WEXIN;
            } else {
                mCbWeixin.setChecked(false);
                defaultPay = 0;
            }

        }
    }

    public class ZhifubaoOncheckListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b) {
                mCbZhifubao.setChecked(true);
                mCbWeixin.setChecked(false);
                defaultPay = PAY_ALI;
            } else {
                mCbZhifubao.setChecked(false);
                defaultPay = 0;
            }
        }
    }
}